package com.fc.mall.portal.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * Created by dobuy on 2019/1/29.
 */
public class DateUtil {

    /**
     * 从Date类型的时间中提取日期部分
     */
    public static Date getDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 从Date类型的时间中提取时间部分
     */
    public static Date getTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, 1970);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static Date addYears(final Date originalDate, final int numYears) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(originalDate);
        cal.add(Calendar.YEAR, numYears);
        return new Date(cal.getTimeInMillis());
    }

    public static Date addMonths(final Date originalDay, final int numMonths) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(originalDay);
        cal.add(Calendar.MONTH, numMonths);
        final Date newDay = new Date(cal.getTimeInMillis());

        return newDay;
    }

    public static Date addDays(final Date originalDay, final int numDays) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(originalDay);
        cal.add(Calendar.DAY_OF_MONTH, numDays);
        final Date newDay = new Date(cal.getTimeInMillis());

        return newDay;
    }

    public static Date addHours(final Date originalDay, final int numHours) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(originalDay);
        cal.add(Calendar.HOUR_OF_DAY, numHours);
        final Date newDay = new Date(cal.getTimeInMillis());

        return newDay;
    }

    public static Date addMinutes(final Date originalDay, final int numMinute) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(originalDay);
        cal.add(Calendar.MINUTE, numMinute);
        final Date newDay = new Date(cal.getTimeInMillis());
        return newDay;
    }

    public static Date addSeconds(final Date originalDay, final int numSeconds) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(originalDay);
        cal.add(Calendar.SECOND, numSeconds);
        return new Date(cal.getTimeInMillis());
    }

}
